// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// o [author] rhyme_lph
// a [email]  rhymelph@gmail.com
// y [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/format/service_and_product_entity.dart';
import 'package:finance_app/src/utils/input_formatter_tools.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/button/normal_text_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/form/gst_inclusive_form.dart';
import 'package:finance_app/src/widgets/form/value_select_form.dart';
import 'package:finance_app/src/widgets/input/form_input_field.dart';
import 'package:finance_app/src/widgets/wrapper/normal_form_wrapper.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/dashboard/credit_debit/item_amount_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:flutter/services.dart';
import 'package:tuple/tuple.dart';

@RouterMeta(paramName: "itemAmountPage")
class ItemAmountPage extends StatefulWidget {
  final ServiceAndProductEntity item;
  final bool isCredit;
  final bool isEdit;

  const ItemAmountPage({
    Key key,
    @required this.item,
    @required this.isCredit,
    this.isEdit,
  }) : super(key: key);

  @override
  _ItemAmountPageState createState() => _ItemAmountPageState();
}

class _ItemAmountPageState extends State<ItemAmountPage>
    with PagePresenter<ItemAmountPage, ItemAmountProvider> {
  @override
  void initState() {
    super.initState();
    provider.initData(widget.item, isEdit: widget.isEdit);
  }

  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
          title:
              widget.isCredit ? 'Item & Credit Amount' : 'Item & Debit Amount',
          actions: [
            if (provider.isShowDelete) buildDeleteBtn(),
          ]);

  Widget buildDeleteBtn() {
    return NormalDeleteTextButton(
      onTap: provider.onDelete,
    );
  }

  @override
  Widget get buildWidget => ListView(
        children: [
          listBuilder<String>(
            selector: (_, __) => widget.item.name,
            builder: (BuildContext context, value, Widget child) =>
                ValueSelectForm(
              // onTap: provider.onSelectCurrency,
              title: S.current.name,
              value: value,
            ),
          ),
          FromInputField(
            tip: widget.isCredit ? 'Credit Amount' : 'Debit Amount',
            controller: provider.amountController,
            focusNode: provider.amountFocusNode,
            inputFormatterList: [
              doubleInputFormatter,
            ],
            keyboardType: TextInputType.numberWithOptions(decimal: true),
          ),
          if (Config.currentCompany.enableGst == 1)
            listBuilder<String>(
              selector: (_, __) => __.gstRate?.displayName,
              builder: (BuildContext context, String value, Widget child) =>
                  ValueSelectForm(
                onTap: provider.onSelectGstRate,
                title: S.current.gst_rate,
                value: value,
              ),
            ),
          listBuilder<bool>(
            selector: (_, __) =>
                __.enable && Config.currentCompany.enableGst == 1,
            builder: (BuildContext context, bool value, Widget child) =>
                value == true
                    ? listBuilder<Tuple3<bool, String, String>>(
                        selector: (_, __) => Tuple3(__.isCheckInclusive,
                            __.getGSTInfo(), __.getTotal()),
                        builder: (BuildContext context,
                                Tuple3<bool, String, String> value,
                                Widget child) =>
                            NormalFormWrapper(
                          child: GstInclusiveForm(
                            isCheckInclusive: value.item1,
                            unit: Config.defaultCurrency,
                            gstInfo: value.item2,
                            total: value.item3,
                            onChangeInclusive: provider.onChangeInclusive,
                          ),
                        ),
                      )
                    : Container(),
          ),
          AppSpaces.h30,
          listBuilder<bool>(
            selector: (_, __) => __.enable,
            builder: (BuildContext context, bool value, Widget child) =>
                NormalButton(
              title: S.current.done,
              onTap: value == true ? provider.onNavDone : null,
            ),
          ),
        ],
      );
}
